﻿using System;
using System.Collections.Generic;
using GameEditor.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.Model;
using GameEngine.View;

namespace GameEditor.View
{
    public class ViewPropertiesEditor_MenuMediator<MyIdType>
        : MenuMediator<IViewEditorMenuViewConstants.ButtonTags, IViewEditorMenuView<MyIdType>>
        where MyIdType : IComparable<MyIdType>
    {
        //public readonly String CREATEITEM_BUTTONPRESSED = "ItemEditorMenuMediator_CREATEITEM_";
        //public readonly String ADDITEM_BUTTONPRESSED = "ItemEditorMenuMediator_ADDITEM_";
        //public readonly String ADDSHAPE_BUTTONPRESSED = "ItemEditorMenuMediator_ADDSHAPE_";
        //public readonly String REMOVESHAPE_BUTTONPRESSED = "ItemEditorMenuMediator_REMOVESHAPE_";

        IViewDataProxy<MyIdType> dataProxy;

        public ViewPropertiesEditor_MenuMediator(String name, IViewEditorMenuView<MyIdType> viewComponent, IViewDataProxy<MyIdType> dataProxy, string outCommand)
            : base(name, viewComponent, outCommand)
        {
            this.dataProxy = dataProxy;
            //CREATEITEM_BUTTONPRESSED += name;
            //ADDITEM_BUTTONPRESSED += name;
            //ADDSHAPE_BUTTONPRESSED += name;
            //REMOVESHAPE_BUTTONPRESSED += name;
        }

        public new IViewEditorMenuView<MyIdType> ViewComponent
        {
            get { return base.ViewComponent as IViewEditorMenuView<MyIdType>; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            return new List<string>();
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            ViewComponent.OnCreateClick += new Action<string>(ViewComponent_OnCreateItemClick);
            ViewComponent.OnEditClick += new Action<string>(ViewComponent_OnEditClick);
            ViewComponent.OnRemoveClick += new Action<string>(ViewComponent_OnRemoveClick);
            ViewComponent.OnAddShapeClick += new Action<string, string, object>(ViewComponent_OnAddShapeClick);
            ViewComponent.OnRemoveShapeClick += new Action<string, string>(ViewComponent_OnRemoveShapeClick);
            //ViewComponent.OnAddItemToWorldClick += new Action<string>(ViewComponent_OnAddItemClick);
            //ViewComponent.OnItemTypeClick += new Action<string>(ViewComponent_OnItemTypeClick);
            ViewComponent.OnNewClick += new Action(ViewComponent_OnNewClick);
        }
        
        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
        }

        #endregion

        void ViewComponent_OnCreateItemClick(string obj)
        {
            //SendNotification(CREATEITEM_BUTTONPRESSED, null, obj);
            //GameWorldProxy.ItemTypes.Add(new KeyValuePair<string, IThingTypeDataProxy<MyIdType>>(
            //    obj,
            //    OnBuildThingType));
            //ViewComponent.AddTypeToList(obj);
        }

        void ViewComponent_OnAddItemClick(string obj)
        {
            //SendNotification(
            //    ADDITEMTOGAME_NOTIFICATION,
            //    ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(
            //        GameWorldProxy.ItemTypes[obj]
            //    ),
            //    null);
            //SendNotification(ADDITEM_BUTTONPRESSED, null, obj);
            //WorldProxy.AddItem(
            //    ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(
            //        WorldProxy.ItemTypes[obj]
            //    ));
        }

        void ViewComponent_OnAddShapeClick(string itemTypeName, string shapeType, object shapeProps)
        {
            //SendNotification(ADDSHAPE_BUTTONPRESSED, arg2, arg1);
            //ThingViewShapeType ShapeType = new ThingViewShapeType(shapeType) { Properties = shapeProps };
            //IThingTypeViewShapeVO<MyIdType> shapeVO = ModelFacade<ItemVOId>.Instance.CreateItemViewShapeVO(ShapeType);

            //IThingTypeDataProxy<MyIdType> itemType;
            //if (GameWorldProxy.ItemTypes.ContainsKey(itemTypeName))
            //{
            //    itemType = GameWorldProxy.ItemTypes[itemTypeName];
            //}
            //else {
            //    if (OnBuildThingType == null || OnBuildThingType.Name != itemTypeName)
            //        OnBuildThingType = ModelFacade<ItemVOId>.Instance.CreateThingTypeDataProxy(itemTypeName);
            //    itemType = OnBuildThingType;
            //}
            //itemType.AddShape(shapeVO);
            ////Update shape menu
            //ViewComponent.AddShapeToList(ShapeType.Name + " - " + shapeVO.Id);

            //IThingDataProxy<MyIdType> item = ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(itemType);

            //ItemEditorWorldMediator.proxy.RemoveAllItems();//TODO ver melhor maneira sem ser tirar tudo
            //item.Position = ItemEditorWorldMediator.proxy.Map.CameraPosition;
            //ItemEditorWorldMediator.proxy.AddItem(item);
        }

        void ViewComponent_OnRemoveShapeClick(string itemTypeName, string shapeName)
        {
            //MyIdType id = ModelFacade<ItemVOId>.Instance.ParseId(shapeName.Split(" - ".ToCharArray())[1]);
            ////SendNotification(REMOVESHAPE_BUTTONPRESSED, arg2, arg1);
            //if (GameWorldProxy.ItemTypes.ContainsKey(itemTypeName))
            //{
            //    GameWorldProxy.ItemTypes[itemTypeName].RemoveShape(id);
            //}
            //else {
            //    if (OnBuildThingType == null || OnBuildThingType.Name != itemTypeName)
            //        OnBuildThingType = ModelFacade<ItemVOId>.Instance.CreateThingTypeDataProxy(itemTypeName);
            //    OnBuildThingType.RemoveShape(id);
            //}
            ViewComponent.RemoveShapeFromList(shapeName);
        }

        void ViewComponent_OnRemoveClick(string obj)
        {
            throw new NotImplementedException();
        }

        void ViewComponent_OnEditClick(string obj)
        {
            throw new NotImplementedException();
        }

        void ViewComponent_OnNewClick()
        {
            //ItemEditorWorldMediator.proxy.RemoveAllItems();
            ViewComponent.RemoveAllShapesFromList();
        }

        void ViewComponent_OnItemTypeClick(string obj)
        {
            //TODO adicionar type ao itemEditorWorldMediator 
            //IThingTypeDataProxy<MyIdType> itemType = GameWorldProxy.ItemTypes[obj];
            //ItemViewerWorldMediator.proxy.RemoveAllItems();
            //IThingDataProxy<MyIdType> item = ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(itemType);
            //ItemViewerWorldMediator.proxy.AddItem(item);
        }
    }
}